package adaptive.opencom;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import OpenCOM.IConnections;
import OpenCOM.ILifeCycle;
import OpenCOM.IMetaInterface;
import OpenCOM.IUnknown;
import OpenCOM.OCM_MultiReceptacle;
import OpenCOM.OpenCOMComponent;

/*
 * The component AdaptiveAppComponent is responsible for adaptive operations in the applications
 * */
public class AdaptiveAppComponent extends OpenCOMComponent
	implements IAdaptiveApp, IUnknown, ILifeCycle, IMetaInterface, IConnections {

	// Our AdaptiveAppComponent can have many policy of adaptations
	public OCM_MultiReceptacle<IAdaptationPolicy> rcAdaptationPolicy;  
	
	public AdaptiveAppComponent(IUnknown mpIOCM) {
		super(mpIOCM);
		
		this.rcAdaptationPolicy = new OCM_MultiReceptacle<IAdaptationPolicy>(IAdaptationPolicy.class);
	}

	// IConnections operations
	@Override
	public boolean connect(IUnknown pSinkIntf, String riid, long provConnID) {
		if(riid.equalsIgnoreCase("adaptive.opencom.IAdaptationPolicy")) {
			return this.rcAdaptationPolicy.connectToRecp(pSinkIntf, riid, provConnID);
		}
		
		return false;
	}

	@Override
	public boolean disconnect(String riid, long connID) {
		if(riid.equalsIgnoreCase("adaptive..ime.IAdaptationPolicy"))
			return this.rcAdaptationPolicy.disconnectFromRecp(connID);
		
		return false;
	}

	// ILifeCycle operations
	@Override
	public boolean startup(Object data) {
		return true;
	}

	@Override
	public boolean shutdown() {
		return true;
	}

	@Override
	public List<AdaptationRequest> adaptationRequest() {		
		// Verify with any of the AdaptationPolicy has a adaptation request
		List<AdaptationRequest> requests = new ArrayList<AdaptationRequest>();
		int size = this.rcAdaptationPolicy.interfaceList.size();
		//Log.d("Size", String.valueOf(size));
		// Add all the adaptation request to the list of the request
		for(int i=0; i<size; i++)
			requests.add(this.rcAdaptationPolicy.interfaceList.get(i).adaptationRequest());
		
		return requests;
	}
}
